package com.dajingling.controller;

import com.dajingling.bean.Bills;
import com.dajingling.bean.Billtype;
import com.dajingling.service.BillTypeService;
import com.dajingling.service.BillsService;
import com.dajingling.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class BillsController {
    @Resource
    private BillsService billsService;
    @Resource
    private BillTypeService billTypeService;

    @RequestMapping("/show")
    public String getTypes(){
        System.out.println("查询页面已跳转！");
        return "show";
    }

    @RequestMapping("/getbillstype")
    public String getBillsTypes(ModelMap modelMap){
        List<Billtype> billtypes = billTypeService.getBillTypes();
        modelMap.addAttribute("types",billtypes);
        System.out.println("记账页面已跳转！");
        return "add";
    }

    @RequestMapping(value = "/insertbills",method = RequestMethod.POST)
    public String insertBills(Bills bills){
        System.out.println("插入:"+bills);
        int insert = billsService.insert(bills);
        if (insert>0){
            System.out.println("插入成功！");
            return "index";
        }
        System.out.println("插入失败！");
        return "redirect:/getbillstype";
    }

    @RequestMapping(value = "/updatebills",method = RequestMethod.POST)
    public String updateBills(Bills bills){
        System.out.println("更新:"+bills);
        int update = billsService.updateByPrimaryKey(bills);
        if (update>0){
            System.out.println("更新成功！");
            return "index";
        }
        System.out.println("更新失败！");
        return "redirect:/getbillstype";
    }

    @RequestMapping("/findbyid")
    public String findById(int bid,ModelMap modelMap){
        Bills bills = billsService.selectByPrimaryKey(bid);
        System.out.println("获取的对象"+bills);
        List<Billtype> billtypes = billTypeService.getBillTypes();
        modelMap.addAttribute("bills",bills);
        modelMap.addAttribute("types",billtypes);
        System.out.println("跳转到更新页面！");
        return "update";
    }

    //删除操作
    @RequestMapping("/deletebyid")
    public void delete(int bid, HttpServletResponse response){
        int i = billsService.deleteByPrimaryKey(bid);
        response.setContentType("text/html;charset=utf-8");
        try {
            PrintWriter writer = response.getWriter();
            if(i>0){
                writer.print("<script>alert('删除成功');location.href='/gettypes'</script>");
                return;
            }
            writer.print("<script>alert('删除失败');location.href='/gettypes'</script>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/gettypes")
    public String getTypes(ModelMap modelMap){
        //1.查询所有的类型
        PageInfo<Bills> info = billsService.getBills(-1,null,null,1, PageUtil.PAGE_SIZE);
        //2.查询所有的账单
        List<Billtype> billtypes = billTypeService.getBillTypes();
        //3.保存数据给前台
        modelMap.addAttribute("info",info);
        modelMap.addAttribute("billtypes",billtypes);
        System.out.println("数据已给"+info+billtypes);
        return "show";
    }

    @RequestMapping("/getallbills")
    public String getAllBills(@RequestParam(defaultValue = "1") int index,@RequestParam(defaultValue = "-1") Integer typeid, String begin, String end, ModelMap modelMap){
        PageInfo<Bills> info = billsService.getBills(typeid,begin,end,index,PageUtil.PAGE_SIZE);
        List<Billtype> billtypes = billTypeService.getBillTypes();
        modelMap.addAttribute("info",info);
        modelMap.addAttribute("billtypes",billtypes);
        modelMap.addAttribute("tid",typeid);
        modelMap.addAttribute("begintime",begin);
        modelMap.addAttribute("endtime",end);
        System.out.println("数据已给"+info+billtypes);
        return "show";
    }
}
